
var  katex = require("./katex-0.13.18/katex.js");
//var  katex = require("../katex-0.13.18/katex.js");

var options = {"displayMode":true,
  //"leqno":false,
  //"fleqn":false,
  "throwOnError":true,
  "errorColor":"#cc0000",
  "strict":"warn","output":"htmlAndMathml",
  "trust":false,
  //"macros":{"\\f":"#1f(#2)"}
};



function tex2html(tex ){ 
  var output = katex.renderToString(tex, options  );
  return output ; 
}


// 测试
/**
 
> the heat semi-group let $$1\leq r\leq p\leq +\infty$$, then
> $$
> \| \nabla^{k} H(t) f\|_p \approx t^{-\frac{k}{2} - \frac{n}{2}(\frac{1}{r} - \frac{1}{p})}\| f\|_r , \quad k = 0 , 1 , t > 0
> $$

 处理箭头的符号

 > \| \nabla^{k} H(t) f\|_p \approx t^{-\frac{k}{2} - \frac{n}{2}(\frac{1}{r} - \frac{1}{p})}\| f\|_r , \quad k = 0 , 1 , t > 0
> 

a = '\n> \\| \\nabla^{k} H(t) f\\|_p \\approx t^{-\\frac{k}{2} - \\frac{n}{2}(\\frac{1}{r} - \\frac{1}{p})}\\| f\\|_r , \\quad k = 0 , 1 , t > 0\n> '

 * */
function remove_markdown_symbol_in_equation(tex){
  //if( tex[0] === '\n' ){
    //
  //}  
  if( tex.substring(0,2) == '\n>' ){
    //var a = tex.replace(  '\n>' ,'' );
    var new_tex= tex.replace(/\n>/g, '\n');
    //console.log(  '方程 新tex 为 ', new_tex );
    return new_tex ; 
  }
  return tex ; 
}

//var last_error_block= [] ;

var last_error_block_tex = '' ;
function try_merge_error_block_tex( block){
  var tex =   last_error_block_tex +  block.body ;
  try { 
    //var output =  tex2html(tex) ;
    options.displayMode = true ;
    var output = katex.renderToString(tex, options  );
    //console.log('tex的api 处理',tex);
    /*
    output = katex.renderToString(tex, {
          displayMode: !isInline
    });
    */ 
    last_error_block_tex = '' ;
    console.log('解决了前几次的错误😊', tex ) ;
    return output
  }
  catch(err) { 
    last_error_block_tex = tex ;
    return false ;
  }

}

function check_error_block_tex(){
  if(  last_error_block_tex != '' ){

    console.log('\n\n\n==============\n') ;
    console.log('前几次的错误尚未解决🙁', last_error_block_tex ) ;
    console.log('==============\n\n\n\n') ;
 

    last_error_block_tex = '';
  }

}


function on_block_tex(tex_block){
  //var isInline = !(tex[0] == "\n");
  var tex = tex_block.body ;
  var displayMode = (tex[0] == "\n");
  tex = remove_markdown_symbol_in_equation(tex) ;
  
  try { 
    //var output =  tex2html(tex) ;
    options.displayMode = displayMode ;
    var output = katex.renderToString(tex, options  );
    //console.log('tex的api 处理',tex);
    /*
    output = katex.renderToString(tex, {
          displayMode: !isInline
    });
    */
    check_error_block_tex() ;
    
    //last_error_block =  [] ;
    return output
  }
  catch(err) {
    console.log('解析发生错误 tex block = ', tex_block );
    console.log('  err = ', err );

    // 尝试拼凑之前的string
    var output_try = try_merge_error_block_tex(tex_block) ;

    if(  output_try ){
      return output_try ;
    }
 
    return  '' ;
    //return err.message;
  }
  
}


/****** 纠正 $  为 $$  ******/
String.prototype.replaceAll = function(search, replacement) {
    var target = this;
    return target.replace(new RegExp(search, 'g'), replacement);
};
function correct_dollar_symbol(page){ 
  var DOLLAR_TIME2= 'D_O_A_L_A2_q_Time' ;
  var b = page.replaceAll( /\$\$/g, DOLLAR_TIME2).replaceAll( /\$/g, DOLLAR_TIME2)
  //c =b.replaceAll( /D_O_A_L_A2_q_Time/g,  "$$$$") ;
  return  b.replaceAll( /D_O_A_L_A2_q_Time/g,  "$$$$") ;
 
}

function fix_page_content( page  ){
  if(  page.content.indexOf('$')  < 0 ){
      console.log(page.path, '无数学公式'   );
      return page ; 
  } 
  console.log( '   纠正',page.path   ,'行内的数学公式');
  //console.log('page的页面为', page) ;
  page.content = correct_dollar_symbol( page.content) ;
  //console.log(page.path, '新的表达式为',page   );
  //console.log('新的页面为',page) ;
  return page 
}

function hifun(){ 
  console.log('\n\n\n==============\n') ;
  console.log('hi \(^o^)/~ 测试调用😃') ;
  console.log('==============\n\n\n\n') ;
}


module.exports = { 
    tex2html: tex2html, 
    fix_page_content: fix_page_content,
    on_block_tex: on_block_tex,
    hi: hifun
};
